// pages/login1/login1.js
const api = require('../../request/request.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    departmentName:"",
    departmentId:"",
    objectArray:[],
    isShow:false,
    status:"0",//0：访客；1：医生
    tel:"",
    password:""
  },
  /*科室选择*/
  bindPickerChange(e){
    let departmentName = this.data.objectArray[e.detail.value]
    let departmentId = '';
    this.data.departmentList.forEach(el=>{
      if(el.departmentName==departmentName){
        departmentId = el.id
      }
    })
    console.log('picker发送选择改变，携带值为', departmentId)
    wx.removeStorageSync('departmentId')
    wx.removeStorageSync('departmentName')
    wx.setStorageSync('departmentId', departmentId)
    wx.setStorageSync('departmentName', departmentName)
    this.setData({
      departmentName:departmentName,
      departmentId:departmentId
    })
  },
     /*
    获取科室
    */
   getDepartment(){
    let url ="/department/getByMapList"
      api.AjaxGet(url,(res)=>{
        console.log(res)
        if(res.code=="10000"){
          let data = res.data
          let list = []
          data.forEach(v=>{
            console.log(v)
            list.push(v.departmentName)
          })
          // this.data.list = res.data.data
          this.setData({
            // departmentName:list[0],
            departmentList: res.data,
            objectArray:list,
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
        }
        })
   },
  
  changeTel(e){
    this.setData({tel:e.detail.value})
  },
  changePassw(e){
    this.setData({
      password:e.detail.value
    })
  },
  login(){
    let status = wx.getStorageSync('status')
    if(status==1){//医生登录
      if(this.data.tel&&this.data.password&&this.data.departmentId){
      let params ={
      accountNumber:this.data.tel,
      pwd:this.data.password,
      departmentId:this.data.departmentId
      };
        wx.showLoading({title: '登录中…'})
        let url ="/doctor/login"
      api.AjaxPost(url,params,(res)=>{
        console.log(res)
        wx.hideLoading()
        if(res.code=="10000"){
          // this.data.list = res.data.data
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
          wx.navigateTo({
            url: '../lineList/lineList',
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none',
            duration: 2000
          })
        }
        })
        }else{
          wx.showToast({
            title: '信息不完整',
            icon: 'none',
            duration: 2000
          })
        }
    }else{//访客登录
      if(this.data.tel.length==11&&this.data.departmentName){
        wx.showLoading({title: '登录中…'})
        
          setTimeout(()=>{
            // wx.navigateTo({
            //   url: '../lineList/lineList',
            // })
            wx.navigateBack({
              delta: 2,
            })
            wx.hideLoading()
          },1500)
      }else{
        wx.showToast({
          title: '信息不完整',
          icon: 'none',
          duration: 2000
        })
      }
    }
    
  },
  goback(e){
    this.setData({
      tel:"",
      password:"",
      departmentName:"",
      departmentId:"",
    })
    wx.navigateBack({
      delta: 1,
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.removeStorageSync('departmentId')
    wx.removeStorageSync('departmentName')
    this.setData({
      status:wx.getStorageSync('status')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getDepartment()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.removeStorageSync('departmentId')
    wx.removeStorageSync('departmentName')
    this.setData({
      status:wx.getStorageSync('status')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})